import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;


public class Server {
	private static final int PORT = 7789;
	private ServerSocket socket;
	private Socket client;
	public static HashMap<Integer,Station> stations = new HashMap<Integer,Station>();
	public static int droppedMeasurements;
	public static int missingValuesCorrected;
	public static int stationFiles;
	public static int measurementsWrittenToFile;
	public static int tempratureCorrections;
	
	
	
	private ThreadPool pool;
	private DatabaseThread db;
	
	public DatabaseThread getDb() {
		return db;
	}

	public Server() {
		//Generate threadpool
		pool = new ThreadPool(800);
		
		//Initialize database
		db = new DatabaseThread();
		Thread dbtrhead = new Thread(db);
		dbtrhead.setPriority(Thread.NORM_PRIORITY+3);
		dbtrhead.start();
		
		new Thread(new Stats(db)).start();
		
		//Listen for connections
		listen();
		
	}
	
	public void listen() {
		try {
			
			socket = new ServerSocket(PORT);
    		
    		// Listen for connections
    		while(true) {	  
    			client = socket.accept();
    			ClientThread clientThread = new ClientThread(client,db, this);
    			pool.add(clientThread);
    		}
	    } catch (IOException e){
	    	System.err.println(e);
	    }
	}
}
